package cn.breeze.pet.listener;

import cn.breeze.order.domain.OrderPetAcquisition;
import cn.breeze.order.mapper.OrderPetAcquisitionMapper;
import cn.breeze.pet.domain.SearchMasterMsg;
import cn.breeze.pet.mapper.PetMapper;
import cn.breeze.pet.mapper.SearchMasterMsgMapper;
import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RocketMQMessageListener(consumerGroup ="order-delay",topic = "order-topic-delay")
public class SearchMsgRocketMQListener implements RocketMQListener<String> {

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;

    @Override
    public void onMessage(String s) {
        // 获取参数
        Map<String,Object> orderMap = JSONObject.parseObject(s, Map.class);
        String orderNumber = String.valueOf(orderMap.get("orderNumber"));
        String createTime = String.valueOf(orderMap.get("createTime"));
        // 根据订单号去查询订单状态，若为为确认，则取消订单，并把消息放入到寻主池
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadByOrderSn(orderNumber);
        if (orderPetAcquisition.getState() == 0){
            // 修改订单状态为取消
            orderPetAcquisition.setState(-1);
            orderPetAcquisitionMapper.update(orderPetAcquisition);
            // 根据id去修改寻主消息状态为1,shopId置空
            SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.loadById(orderPetAcquisition.getSearchMasterMsgId());
            searchMasterMsg.setShopId(null);
            searchMasterMsg.setState(1);
            searchMasterMsgMapper.update(searchMasterMsg);
            // 删除宠物信息
            petMapper.remove(orderPetAcquisition.getPetId());
        }
        System.out.println("编号为："+orderNumber + ":创建消息时间："+createTime);
    }
}
